package com.mengya.interrupt;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
import java.util.stream.IntStream;

/**
 * @ClassName T1
 * @Description
 * @Author xiongwei.wu
 * @Date 2023/1/13 9:09
 **/
public class T1 {
    private static List<Integer> list1 = new ArrayList<>();
    private static List<Integer> list2 = new ArrayList<>();
    private static List<Integer> list3 = new ArrayList<>();
    private static List<Integer> list4 = new ArrayList<>();
    private static Lock lock = new ReentrantLock();

    public static void main(String[] args) {
        IntStream.range(0, 10000).forEach(list1::add);
        IntStream.range(0, 10000).parallel().forEach(value -> list2.add(value));
        IntStream.range(0, 10000).forEach(value -> {
            lock.lock();
            try {
                list3.add(value);
            } finally {
                lock.unlock();
            }
        });
        IntStream.range(0, 10000).parallel().forEach(value -> {
            lock.lock();
            try {
                list4.add(value);
            } finally {
                lock.unlock();
            }
        });

        System.out.println("串行执行的大小：" + list1.size());
        System.out.println("并行执行的大小：" + list2.size());
        System.out.println("加锁串行执行的大小：" + list3.size());
        System.out.println("加锁并行执行的大小：" + list4.size());
    }
}
